﻿using System;
using ERP_RORZE_SYSTEM;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_ExchangeRate : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;
        public string exchange_id;

        public frm_Edit_ExchangeRate()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            exchange_id = null;
            this.addDate.EditValue = DateTime.Now.Date;
            LoadCurrencyList();
            
        }
        //////////////////////////////////////
        /// Load Data
        //////////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(exchange_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT CONVERT(DATETIME, ExchangeDate, 112) AS ExchangeDate, CurrencyID, ExchangeRate"
                        + " FROM [222_ExchangeRate] WHERE ID=" + exchange_id;
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.addDate.EditValue = DateTime.Parse(dataReader["ExchangeDate"].ToString()).Date;
                        for (int i = 0; i < this.currencyID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.currencyID.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["CurrencyID"].ToString())
                            {
                                this.currencyID.SelectedIndex = i;
                            }
                        }
                        this.exchangeRate.Text = dataReader["ExchangeRate"].ToString();
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                    ret = false;
                }
            }
            return ret;
        }


        //////////////////////////////////////
        /// Load Currency List
        //////////////////////////////////////
        private void LoadCurrencyList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CurrencyID, CurrencyName, CurrencySymbol FROM [222_Currency] ORDER BY CurrencySymbol";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["CurrencySymbol"].ToString() + " | " + dataReader["CurrencyName"].ToString();
                    item.Value = dataReader["CurrencyID"].ToString();
                    this.currencyID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Currency list");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string currency_id = null;
            string exchang_rate = null;
            DateTime add_date = new DateTime();
            float rate = 0;

            if (this.addDate.EditValue == null)
            {
                error = "Please select Date";
            }
            else
            {
                add_date = DateTime.Parse(this.addDate.EditValue.ToString());
            }
            if (string.IsNullOrEmpty(error))
            {
                if (this.currencyID.SelectedIndex < 0)
                {
                    error = "Please select Currency";
                }
                else
                {
                    ComboboxItem item = (ComboboxItem)this.currencyID.SelectedItem;
                    currency_id = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.exchangeRate.Text))
                {
                    error = "Please enter Exchange Rate";
                }
                else
                {
                    if (!float.TryParse(this.exchangeRate.Text, out rate))
                    {
                        error = "Invalid Exchange Rate";
                    }
                    else
                    {
                        exchang_rate = this.exchangeRate.Text;
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_ExchangeRate] SET ExchangeDate=@exchangedate, CurrencyID=" + currency_id + ","
                        + " ExchangeRate=" + exchang_rate + " WHERE ID=" + exchange_id;                        
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@exchangedate", SqlDbType.NVarChar).Value = add_date.Date.ToString("yyyyMMdd");
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();                    
                    MessageBox.Show("Exchange Rate updated success.");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update Exchange Rate. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
